package pers.qianyu.month_202102.date_20210222;

/**
 * 766. 托普利茨矩阵
 * https://leetcode-cn.com/problems/toeplitz-matrix/
 *
 * @author mizzle rain
 * @date 2021-02-22 10:20
 */
public class IsToeplitzMatrix {
    public boolean isToeplitzMatrix(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        for (int j = 0; j < n - 1; j++) {
            int key = matrix[0][j];
            for (int k = 1, t = j + 1; k < m && t < n; t++, k++) {
                if (matrix[k][t] != key) {
                    return false;
                }
            }
        }
        for (int i = 1; i < m - 1; i++) {
            int key = matrix[i][0];
            for (int k = i + 1, t = 1; t < n && k < m; t++, k++) {
                if (key != matrix[k][t]) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean isToeplitzMatrix2(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (matrix[i][j] != matrix[i - 1][j - 1]) {
                    return false;
                }
            }
        }
        return true;
    }
}
